package db

import (
	"go_my_framework/app/core/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var conn *gorm.DB

func InitDb() {
	var user = config.Instance().DB.Username
	var pass = config.Instance().DB.Password
	var host = config.Instance().DB.Host
	var db = config.Instance().DB.Database
	var port = config.Instance().DB.Port
	var charset = config.Instance().DB.Charset
	dsn := user + ":" + pass + "@tcp(" + host + ":" + port + ")/" + db + "?charset=" + charset + "&parseTime=True&loc=Local"
	dbConn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
		return
	}
	conn = dbConn
}

func Instance() *gorm.DB {
	if conn == nil {
		InitDb()
	}
	return conn
}
